package com.game.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SplitPhoneUtils {

    /**
     * 切割手机号和区号
     * 86开头+11位数，是中国区号+手机号
     * 852开头+8位数，是香港的区号+手机号
     * 853开头+8位数，是澳门的区号+手机号
     * 886开头+9位数，是台湾的区号+手机号
     * 81开头+9位数，是日本的区号+手机号
     * @param mobile
     * @return
     */
    public static JSONObject splitPhone(String mobile) {
        JSONObject jsonObject = new JSONObject();
        String[] regexArray = {
                "^\\+86(\\d{11})$", "^86(\\d{11})$",
                "^\\+852(\\d{8})$", "^852(\\d{8})$",
                "^\\+853(\\d{8})$", "^853(\\d{8})$",
                "^\\+886(\\d{9})$", "^886(\\d{9})$",
                "^\\+81(\\d{9})$", "^81(\\d{9})$",};
        String areaCode = "clear";
        String phone = mobile;
        for (int i = 0; i < regexArray.length; i++) {
            String regex = regexArray[i];
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(mobile);
            if (matcher.matches()) {
                phone = matcher.group(1);
                areaCode = mobile.substring(0, mobile.lastIndexOf(phone));
            }
        }
        jsonObject.put("areaCode", areaCode);
        jsonObject.put("mobile", phone);
        return jsonObject;
    }
}
